<?php
/**
 * Created by PhpStorm.
 * User: mr.houhai
 * Date: 2018/3/18
 * Time: 14:43
 */

namespace Admin\Controller;

class GoodsController extends BaseController
{
    public function add_goods()
    {
        $this->display();
    }

    /**
     * 添加商品
     */
    public function doAddGoods()
    {
        $data['name'] = I('name','');
        $data['intro'] = I('intro','');
        $data['intro2'] = I('intro2','');
        $data['price'] = I('price','');

        $info = uploadImg('goods');
        $data['img'] = $info['img']['savename'];

        $data['content'] = htmlspecialchars_decode(I('content_detail',''));
        $data['sales_volume'] = I('sales_volume','');
        $data['sort'] = I('sort','255');
        $data['state'] = I('state','0');
        $data['add_time'] = time();

        foreach ($data as $k=>$v) {
            if ($v == '' || $v == null)
                echo "<script>alert('{$k},不能为空');location.replace(document.referrer);</script>";
        }

        $res = M('goods')->data($data)->add();

        if ($res) {
            echo "<script>alert('添加成功');location.replace(document.referrer);</script>";
        } else {
            echo "<script>alert('添加失败');location.replace(document.referrer);</script>";
        }
    }

    /**
     * 商品列表
     */
    public function goods_list()
    {
        $list = M('goods')->order(array('sort'=>'asc','add_time'=>'desc'))->select();

        $this->assign('list',$list);
        $this->display();
    }

    public function update_goods()
    {
        $maps['id'] = I('id');
        $data = M('goods')->where($maps)->find();

        $this->assign('data',$data);
        $this->display();
    }

    public function doUpdateGoods()
    {
        $maps['id'] = I('id');

        $data['name'] = I('name','');
        $data['intro'] = I('intro','');
        $data['intro2'] = I('intro2','');
        $data['price'] = I('price','');

        if ($_FILES['img']) {
            $info = uploadImg('goods');
            $data['img'] = $info['img']['savename'];
        }

        $data['content'] = htmlspecialchars_decode(I('content_detail',''));
        $data['sales_volume'] = I('sales_volume','');
        $data['sort'] = I('sort','255');
        $data['state'] = I('state','0');
        $data['add_time'] = time();

        foreach ($data as $k=>$v) {
            if ($v == '' || $v == null)
                echo "<script>alert('{$k},不能为空');location.replace(document.referrer);</script>";
        }

        $res = M('goods')->data($data)->where($maps)->save();

        if ($res) {
            echo "<script>alert('添加成功');location.replace(document.referrer);</script>";
        } else {
            echo "<script>alert('添加失败');location.replace(document.referrer);</script>";
        }

    }

    /**
     * 图片轮播
     */
    public function goods_banner()
    {
        $maps['gid'] = I('id');
        $list = M('goods_banner')->where($maps)->select();

        $this->assign('list',$list);
        $this->display();
    }

    /**
     * 添加商品轮播图
     */
    public function doAddGoodsBanner()
    {
        $data['gid'] = I('id');
        $info = uploadImg('goods_banner');
        $data['img'] = $info['img']['savename'];
        $data['add_time'] = time();

        $res = M('goods_banner')->data($data)->add();
        if ($res) {
            echo "<script>alert('操作成功');location.replace(document.referrer);</script>";
        } else {
            echo "<script>alert('操作失败');location.replace(document.referrer);</script>";
        }
    }

    /**
     * 删除商品轮播
     */
    public function deleteImg()
    {
        $maps['id'] = I('id');
        $res = M('goods_banner')->where($maps)->delete();

        if ($res) {
            echo "<script>alert('操作成功');location.replace(document.referrer);</script>";
        } else {
            echo "<script>alert('操作失败');location.replace(document.referrer);</script>";
        }
    }

    /**
     * 商品规格
     */
    public function goods_spec()
    {
        $maps['gid'] = I('id');
        $list = M('goods_spec')->where($maps)->select();

        $this->assign('list',$list);
        $this->display();
    }

    /**
     * 添加商品规格
     */
    public function doAddGoodsSpec()
    {
        $data['gid'] = I('id');
        $data['name'] = I('name');
        $data['price'] = I('price');
        $data['weight'] = I('weight');
        $data['state'] = I('state','0');
        $data['add_time'] = time();

        foreach ($data as $k=>$v) {
            if ($v == '' || $v == null)
                echo "<script>alert('{$k},不能为空');location.replace(document.referrer);</script>";
        }

        $res = M('goods_spec')->data($data)->add();
        if ($res) {
            echo "<script>alert('操作成功');location.replace(document.referrer);</script>";
        } else {
            echo "<script>alert('操作失败');location.replace(document.referrer);</script>";
        }
    }

    /**
     * 商品规格列表
     */
    public function update_spec()
    {
        $maps['id'] = I('id');
        $data = M('goods_spec')->where($maps)->find();

        $this->assign('data',$data);
        $this->display();
    }

    /**
     * 更新商品规格
     */
    public function updateGoodsSpec()
    {
        $maps['id'] = I('id');

        $data['name'] = I('name');
        $data['price'] = I('price');
        $data['weight'] = I('weight');
        $data['state'] = I('state','0');

        foreach ($data as $k=>$v) {
            if ($v == '' || $v == null)
                echo "<script>alert('{$k},不能为空');location.replace(document.referrer);</script>";
        }

        $res = M('goods_spec')->data($data)->where($maps)->save();
        if ($res) {
            echo "<script>alert('操作成功');location.replace(document.referrer);</script>";
        } else {
            echo "<script>alert('操作失败');location.replace(document.referrer);</script>";
        }
    }
}